package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.dto.input.BoreholeConditionInputDto;
import com.b511.swpu.cementbackend.service.MetaDictService;
import com.b511.swpu.cementbackend.service.PorePressureService;
import com.b511.swpu.cementbackend.dto.input.MetaDictInputDto;
import com.b511.swpu.cementbackend.entity.PorePressure;
import com.b511.swpu.cementbackend.repository.PorePressureRepository;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "地层孔隙压力", tags = {"地层孔隙压力"})
@RestController
@RequestMapping("/pore_pressure")
public class PorePressureController {
    private final PorePressureRepository porePressureRepository;

    private final PorePressureService porePressureService;

    private final MetaDictService metaDictService;

    public PorePressureController(PorePressureRepository porePressureRepository,
                                  PorePressureService porePressureService,
                                  MetaDictService metaDictService) {
        this.porePressureRepository = porePressureRepository;
        this.porePressureService = porePressureService;
        this.metaDictService = metaDictService;
    }

    @GetMapping("/{num}/{size}")
    public R getPorePressureByCondition(@PathVariable int num, @PathVariable int size, @ModelAttribute BoreholeConditionInputDto boreholeConditionInputDto) {
        return porePressureService.getPorePressureByCondition(boreholeConditionInputDto, PageRequest.of(num, size));
    }

    @PutMapping("/{id}")
    public R updatePorePressure(@PathVariable Long id, @RequestBody PorePressure porePressure) {
        return porePressureService.updatePorePressure(id, porePressure);
    }

    @DeleteMapping("/{id}")
    public R deletePorePressure(@PathVariable Long id) {
        porePressureRepository.deleteById(id);
        return R.ok();
    }

    @GetMapping("/dict/child")
    public R getDictByName(@ModelAttribute @Validated MetaDictInputDto metaDictInputDto) {
        return metaDictService.getDictByName(metaDictInputDto.getLabel());
    }
}
